import pygame
from pygame.locals import *

class InnerGear:
	def __init__(self):
		import os
		self.screenStack=[]
		pygame.init()
		pygame.font.init()
		self.music=None
		#self.music_volume=1.0
		self.file='settings.xml'
		if self.file in os.listdir(os.getcwd()):
			self.readSettings()
		else:
			self.setDefault()
			self.writeSettings()
		self.makeWindow()
	def setNewMusicVolume(self,value):
		self.music_volume=value
		pygame.mixer.music.set_volume(value)
	def setDefault(self):
		self.screen_size=pygame.display.list_modes()[0]
		self.locali='en'
		self.music_volume=0.3
		#pass
	def makeWindow(self):
		pygame.display.set_caption("Main cicle testo")
		#screen_size=pygame.display.list_modes()[0]#(1280,1024)
		screen = pygame.display.set_mode(self.screen_size , DOUBLEBUF)
		#screen = pygame.display.set_mode(self.screen_size , DOUBLEBUF|FULLSCREEN)
		self.screen=screen
		#self.screen_size=screen_size
	def writeSettings(self):
		source=open(self.file,'w')
		source.write("<Settings>")
		source.write("<screen width='"+str(self.screen_size[0])+"' height='"+str(self.screen_size[1])+"' />")
		source.write("<language locali='"+self.locali+"' />")
		source.write("<sound music_volume='"+str(self.music_volume)+"' />")
		source.write("</Settings>")
		source.close()
		#pass
	def readSettings(self):
		import xml.dom.minidom 
		#from xml.dom.minidom import Node
		changes=False
		doc = xml.dom.minidom.parse(self.file)
		screens=doc.getElementsByTagName("screen")
		if len(screens)>0:
			width=screens[-1].getAttribute('width')
			if len(width)>0:
				w=int(width)
			else:
				w=0
			height=screens[-1].getAttribute('height')
			if len(height)>0:
				h=int(height)
			else:
				h=0
			size=(w,h)
		else:
			size=(0,0)
		if size in pygame.display.list_modes():
			self.screen_size=size
		else:
			self.screen_size=pygame.display.list_modes()[0]
			changes=True
		sounds=doc.getElementsByTagName("sound")
		if len(sounds)>0:
			music_volume=sounds[-1].getAttribute('music_volume')
			if len(music_volume)>0:
				self.music_volume=float(music_volume)
			else:
				self.music_volume=0.3
				changes=True
		else:
			self.music_volume=0.3
			changes=True
		languages=doc.getElementsByTagName("language")
		if len(languages)>0:
			locali=languages[-1].getAttribute('locali')
			if len(locali)>0:
				self.locali=locali
			else:
				self.locali='en'
				changes=True
		else:
			self.locali='en'
			changes=True
		if changes:
			self.writeSettings()